﻿Imports System.Linq

Public Class _Default
    Inherits System.Web.UI.Page
#Region "Collections"
    Protected WithEvents _calendarList As System.Data.Objects.ObjectSet(Of TimelineVB.Calendar)
    Protected WithEvents _eventList As System.Data.Objects.ObjectSet(Of TimelineVB.Availability)
    Protected WithEvents _matchList As System.Data.Objects.ObjectSet(Of TimelineVB.Match)
#End Region

#Region "Properties"
    Private _currentDateTime As DateTime
    Public Property CurrentDateTime() As DateTime
        Get
            Return _currentDateTime
        End Get
        Set(ByVal value As DateTime)
            _currentDateTime = value
        End Set
    End Property

    Private _CalendarEventList As System.Collections.Generic.List(Of CalendarEvent) = Nothing
    Private ReadOnly Property CalendarEventList As System.Collections.Generic.List(Of CalendarEvent)
        Get
            If _CalendarEventList Is Nothing Then
                _CalendarEventList = New System.Collections.Generic.List(Of CalendarEvent)

                Dim _notAvailableList As System.Collections.Generic.List(Of CalendarEvent) = Nothing
                Dim _availableList As System.Collections.Generic.List(Of CalendarEvent) = Nothing
                Dim _matchEventList As System.Collections.Generic.List(Of CalendarEvent) = Nothing

                Dim _currentData As DateTime = CurrentDateTime
                Dim _calendarEvent As CalendarEvent = Nothing
                Dim gg As Integer = DateTime.DaysInMonth(_currentData.Year, _currentData.Month)
                Dim dateTo As Date
                Dim _idCounter = 1

                For Each _currentCalendar As Calendar In _calendarList
                    dateTo = New Date(_currentData.Year, _currentData.Month, 1).AddDays(-1)
                    _availableList = New System.Collections.Generic.List(Of CalendarEvent)
                    _notAvailableList = New System.Collections.Generic.List(Of CalendarEvent)
                    _matchEventList = New System.Collections.Generic.List(Of CalendarEvent)



                    Dim _localCalendar As Calendar = _currentCalendar
                    Dim _mmList As List(Of TimelineVB.Match) = _matchList.ToList.Where(Function(x) (x.HomeTeamID = _localCalendar.TeamID OrElse _
                                                                                                   x.AwayTeamID = _localCalendar.TeamID)).ToList
                    Dim _mList As List(Of TimelineVB.Match) = _mmList.Where(Function(x) x.DateMatch.Month >= CurrentDateTime.Month AndAlso x.DateMatch.Month <= CurrentDateTime.Month).ToList
                    If _mList IsNot Nothing AndAlso _mList.Count > 0 Then
                        For Each m In _mList
                            Dim _matchEvent As CalendarEvent = New CalendarEvent
                            _matchEvent.eventID = _idCounter
                            _idCounter = _idCounter + 1
                            _matchEvent.calendarID = _localCalendar.ID
                            _matchEvent.eventName = String.Empty
                            _matchEvent.dateFrom = m.DateMatch
                            _matchEvent.dateTo = m.DateMatch
                            _matchEvent.typeEvent = CalendarEvent.eTypeCalendarEvent.MatchEvent
                            _matchEvent.IsMatchDay = True
                            _matchEvent.addictionalInfo.AwayTeamID = m.AwayTeamID
                            _matchEvent.addictionalInfo.HomeTeamID = m.HomeTeamID
                            _matchEvent.addictionalInfo.AwayTeamName = m.AwayTeamName
                            _matchEvent.addictionalInfo.HomeTeamName = m.HomeTeamName

                            _matchEventList.Add(_matchEvent)
                        Next
                    End If

                    Dim _list As List(Of TimelineVB.Calendar) = _calendarList.ToList
                    Dim tolist As List(Of Availability) = _eventList.ToList.Where(Function(x) x.DateFrom.Month >= CurrentDateTime.Month AndAlso x.DateTo.Month <= CurrentDateTime.Month).ToList
                    Dim collection = tolist.Where(Function(x) x.CalendarID = _localCalendar.ID).OrderBy(Function(y) y.DateFrom).ToArray
                    If collection IsNot Nothing AndAlso collection.Length > 0 Then

                        For i As Integer = 0 To collection.Count - 1
                            Dim _currentEvent As Availability = collection(i)
                            If _currentEvent.DateFrom.Subtract(dateTo).Days > 1 Then
                                Dim _notAvailabilityEvent As CalendarEvent = New CalendarEvent
                                _notAvailabilityEvent.eventID = _idCounter
                                _idCounter = _idCounter + 1
                                _notAvailabilityEvent.calendarID = _currentEvent.CalendarID
                                _notAvailabilityEvent.eventName = String.Empty
                                _notAvailabilityEvent.dateFrom = dateTo.AddDays(1)
                                _notAvailabilityEvent.dateTo = _currentEvent.DateFrom.AddDays(-1)
                                _notAvailabilityEvent.typeEvent = CalendarEvent.eTypeCalendarEvent.NotAvailable

                                _notAvailableList.Add(_notAvailabilityEvent)
                            End If

                            _calendarEvent = New CalendarEvent
                            _calendarEvent.eventID = _idCounter
                            _idCounter = _idCounter + 1
                            _calendarEvent.fgenEventID = _currentEvent.ID
                            _calendarEvent.calendarID = _currentEvent.CalendarID
                            _calendarEvent.dateFrom = _currentEvent.DateFrom
                            _calendarEvent.dateTo = _currentEvent.DateTo
                            _calendarEvent.typeEvent = CalendarEvent.eTypeCalendarEvent.AvailabilityEvent
                            _availableList.Add(_calendarEvent)

                            dateTo = _calendarEvent.dateTo

                            If i = collection.Count - 1 AndAlso _currentEvent.DateTo.Day < gg Then
                                Dim _notAvailabilityEvent As CalendarEvent = New CalendarEvent
                                _notAvailabilityEvent.eventID = _idCounter
                                _idCounter = _idCounter + 1
                                _notAvailabilityEvent.calendarID = _currentEvent.CalendarID
                                _notAvailabilityEvent.eventName = String.Empty
                                _notAvailabilityEvent.dateFrom = _currentEvent.DateTo.AddDays(1)
                                _notAvailabilityEvent.dateTo = New DateTime(_currentData.Year, _currentData.Month, gg)
                                _notAvailabilityEvent.typeEvent = CalendarEvent.eTypeCalendarEvent.NotAvailable

                                _notAvailableList.Add(_notAvailabilityEvent)
                            End If
                        Next
                    Else
                        Dim _notAvailabilityEvent As CalendarEvent = New CalendarEvent
                        _notAvailabilityEvent.eventID = _idCounter
                        _idCounter = _idCounter + 1
                        _notAvailabilityEvent.calendarID = _localCalendar.ID
                        _notAvailabilityEvent.eventName = String.Empty
                        _notAvailabilityEvent.dateFrom = New DateTime(CurrentDateTime.Year, CurrentDateTime.Month, 1)
                        _notAvailabilityEvent.dateTo = New DateTime(CurrentDateTime.Year, CurrentDateTime.Month, DateTime.DaysInMonth(CurrentDateTime.Year, CurrentDateTime.Month))
                        _notAvailabilityEvent.typeEvent = CalendarEvent.eTypeCalendarEvent.NotAvailable

                        _notAvailableList.Add(_notAvailabilityEvent)

                    End If


                    _CalendarEventList.AddRange(_availableList)
                    _CalendarEventList.AddRange(_notAvailableList)
                    _CalendarEventList.AddRange(_matchEventList)

                Next


            End If

            Return _CalendarEventList.OrderBy(Function(x) x.dateFrom).ToList
        End Get
    End Property

#End Region

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim dataModelContainer As TimelineDataModelContainer = New TimelineDataModelContainer

        _calendarList = dataModelContainer.CalendarSet
        _eventList = dataModelContainer.AvailabilitySet
        _matchList = dataModelContainer.MatchSet

        If ViewState("_targetDate") Is Nothing Then
            CurrentDateTime = DateTime.Now
            ViewState("_targetDate") = CurrentDateTime
        Else
            CurrentDateTime = ViewState("_targetDate")
        End If
    End Sub

    Private Sub btn_prev_Click(sender As Object, e As System.Web.UI.ImageClickEventArgs) Handles btn_prev.Click
        CurrentDateTime = CurrentDateTime.AddMonths(-1)
        ViewState("_targetDate") = CurrentDateTime
    End Sub

    Private Sub btn_next_Click(sender As Object, e As System.Web.UI.ImageClickEventArgs) Handles btn_next.Click
        CurrentDateTime = CurrentDateTime.AddMonths(1)
        ViewState("_targetDate") = CurrentDateTime

    End Sub

    Private Sub Page_LoadComplete(sender As Object, e As System.EventArgs) Handles Me.LoadComplete
        RenderTable()

    End Sub

    Private Sub RenderTable()
        Dim gg As Integer = 0
        gg = DateTime.DaysInMonth(CurrentDateTime.Year, CurrentDateTime.Month)
        mText.Text = String.Format("{0} {1}", CurrentDateTime.ToString("MMMM"), CurrentDateTime.Year)

        Dim _row As TableRow = Nothing
        Dim _list As List(Of TimelineVB.Calendar) = _calendarList.ToList

        For i = 0 To _list.Count
            Dim localcalendar As Calendar = Nothing
            Dim local_row_index As Integer = i
            _row = New TableRow

            Dim _cell As TableCell
            For g = 0 To gg
                _cell = New TableCell

                _cell.Style.Add(HtmlTextWriterStyle.PaddingLeft, IIf(g = 0, "5px", "0px"))

                If i = 0 AndAlso g = 0 Then
                    _cell.Style.Add(HtmlTextWriterStyle.FontWeight, "bold")
                    _cell.Text = "Team"
                ElseIf i = 0 Then
                    _cell.CssClass = "_day_cell"
                    _cell.Style.Add(HtmlTextWriterStyle.FontWeight, "bold")
                    _cell.HorizontalAlign = HorizontalAlign.Center
                    Dim currentDay As DateTime = New DateTime(CurrentDateTime.Year, CurrentDateTime.Month, g)
                    If currentDay.DayOfWeek = DayOfWeek.Saturday OrElse currentDay.DayOfWeek = DayOfWeek.Sunday Then
                        _cell.CssClass = String.Format("{0} _weekend", _cell.CssClass)
                    End If

                    _cell.Text = g
                Else
                    If g = 0 Then
                        Dim _link As String = String.Empty
                        _link = String.Format("alert('{0}');", _list(i - 1).TeamName)
                        _cell.Attributes.Add("onclick", _link)
                        _cell.Style.Add(HtmlTextWriterStyle.Color, "blue")
                        _cell.Style.Add(HtmlTextWriterStyle.TextDecoration, "underline")
                        _cell.Text = _list(i - 1).TeamName

                    Else
                        localcalendar = _list(i - 1)
                        Dim localDate = New DateTime(CurrentDateTime.Year, CurrentDateTime.Month, g)
                        Dim localCalendarID As Integer = localcalendar.ID
                        Dim _elist As List(Of CalendarEvent) = CalendarEventList

                        Dim _event = (From item As CalendarEvent In _elist
                                                                         Where item.dateFrom = localDate AndAlso
                                                                                item.calendarID = localcalendar.ID
                                                                         Select item).ToList
                        For Each _ev In _event
                            If _ev IsNot Nothing Then
                                Dim _div As HtmlGenericControl = New HtmlGenericControl("div")
                                Dim _span As HtmlGenericControl = New HtmlGenericControl("span")
                                Dim _class As String = String.Empty
                                Dim _duration As Integer = (_ev.dateTo - _ev.dateFrom).Days
                                If _duration = 0 Then
                                    _duration = 1
                                Else
                                    _duration = _duration + 1
                                End If

                                If _ev.typeEvent = CalendarEvent.eTypeCalendarEvent.AvailabilityEvent Then
                                    _class = "available"
                                    _span.Style.Add(HtmlTextWriterStyle.Width, String.Format("{0}px", (_duration * 25) + (_duration - 1)))
                                ElseIf _ev.typeEvent = CalendarEvent.eTypeCalendarEvent.NotAvailable Then
                                    _class = "not_available"
                                    _span.Style.Add(HtmlTextWriterStyle.Width, String.Format("{0}px", (_duration * 25) + (_duration - 1)))
                                ElseIf _ev.typeEvent = CalendarEvent.eTypeCalendarEvent.MatchEvent Then
                                    _class = "_ball"
                                    _span.Style.Add(HtmlTextWriterStyle.Width, String.Format("{0}px", (_duration * 12) + (_duration - 1)))

                                    Dim img As HtmlImage = New HtmlImage
                                    img.Src = "img/Soccer-icon.png"
                                    img.Width = 16
                                    img.Height = 16
                                    Dim tooltip As String = String.Format("{0} - {1}", _ev.addictionalInfo.HomeTeamName, _ev.addictionalInfo.AwayTeamName)
                                    img.Alt = tooltip
                                    img.Attributes.Add("onmouseover", String.Format("alert('{0}')", tooltip))
                                    _span.Controls.Add(img)
                                End If
                                _div.Attributes.Add("class", _class)
                                _div.Controls.Add(_span)
                                _cell.Controls.Add(_div)
                            Else
                                Dim _div As HtmlGenericControl = New HtmlGenericControl("div")
                                Dim _span As HtmlGenericControl = New HtmlGenericControl("span")
                                _div.Controls.Add(_span)
                                _cell.Controls.Add(_div)
                            End If
                        Next

                    End If
                End If
                _row.Cells.Add(_cell)
            Next
            c_timeline.Rows.Add(_row)
        Next
    End Sub

End Class

Public Class CalendarEvent
    Public Enum eTypeOperation
        None = 0
        [New] = 1
        Edit = 2
        Delete = 3
    End Enum

    Public Enum eTypeCalendarEvent
        AvailabilityEvent
        NotAvailable
        MatchEvent
    End Enum

    Public calendarID As Integer
    Public eventName As String
    Public eventID As Integer
    Public fgenEventID As Integer
    Public dateFrom As DateTime
    Public dateTo As DateTime
    Public typeEvent As eTypeCalendarEvent
    Public addictionalInfo As AddictionalEventInfo
    Public IsMatchDay As Boolean
    Public typeOperation As eTypeOperation

    Public Sub New()
        addictionalInfo = New AddictionalEventInfo
    End Sub

    Public Class AddictionalEventInfo
        Public HomeTeamID As Integer
        Public AwayTeamID As Integer
        Public HomeTeamName As String
        Public AwayTeamName As String
        Public CompetitionShortname As String
    End Class

End Class